package com.hqu.ms.nearby;

import java.util.List;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.hqu.ms.R;
import com.hqu.ms.entities.StoreEntivity;
import com.hqu.ms.entities.ViewHolder;
import com.hqu.ms.utils.Constant;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NearbyArrayAdapter extends ArrayAdapter<StoreEntivity>{

	
	private List<StoreEntivity> stores;
	private Context mContext;
	
	private boolean showDis;
	
	public NearbyArrayAdapter(Context context,List<StoreEntivity> objects, boolean showDistance) {
		
		super(context, R.layout.ms_nearby_list_item, objects);
		stores = objects;
		mContext = context;
		showDis = showDistance;
	}
	
	public void setShowDis(boolean showdis){
		showDis = showdis;
	}
	
	public void setStores(List<StoreEntivity> s){
		stores = s;
	}

	
	@Override
	public int getCount() {
		return stores.size();
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder ;
		if(convertView == null){
			 convertView =  LayoutInflater.from(mContext).inflate(R.layout.ms_nearby_list_item, null);
			 holder = new ViewHolder();
			 holder.imageView = (ImageView)convertView.findViewById(R.id.nearby_shop_list_image);
			 holder.ivBack = (ImageView)convertView.findViewById(R.id.ivBack);
			 holder.titleView = (TextView)convertView.findViewById(R.id.nearby_shop_list_title);
			 holder.locateView = (TextView)convertView.findViewById(R.id.nearby_shop_list_location_text);
			 holder.distanceView = (TextView)convertView.findViewById(R.id.nearby_shop_list_location_distance);
			 holder.lllayout = (LinearLayout)convertView.findViewById(R.id.lllayout);
			 convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}	
		StoreEntivity store = getItem(position);
		holder.titleView.setText(store.getMg_name());
		if(TextUtils.isEmpty(store.getMg_picture())){
			holder.imageView.setVisibility(View.GONE);
			holder.ivBack.setVisibility(View.GONE);
			holder.lllayout.setGravity(Gravity.CENTER);
			holder.titleView.setGravity(Gravity.CENTER);
		}else{
			AQuery query = new AQuery(convertView);
			ImageOptions options = new ImageOptions();
			options.round = 25;
			query.id(holder.imageView).image(Constant.URL_MSGGROUP_SMALL + store.getMg_picture(), options);
//			query.id(holder.imageView)
//			.image(Constant.URL_MSGGROUP_BIG+store.getMg_picture(), true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);
			holder.lllayout.setGravity(Gravity.CENTER | Gravity.LEFT);
			holder.titleView.setGravity(Gravity.CENTER | Gravity.LEFT);
		}
		
		holder.locateView.setText(store.getMg_address());
		holder.store = store;
		if(showDis){
			float dis = (float) store.getMg_juli();
			float show = 0.f;
			if(dis/1000 > 0.0f){
				show = (float) (dis/1000 + (dis%1000.0)/1000);
			}else{
				show = (float) (dis%1000.0/1000);
			}
			
			holder.distanceView.setText(String.format(mContext.getString(R.string.shop_distance_str), show));
		}else{
			holder.distanceView.setVisibility(View.GONE);
		}

		return convertView;
	}
	
	
}
